package com.qili.controller.base;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.core.annotation.Log;
import com.qili.entity.base.BaseProgram;
import com.qili.entity.base.dto.BaseProgramDTO;
import com.qili.exception.MyException;
import com.qili.service.base.BaseProgramService;
import com.qili.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sys/base/programManager")
public class BaseProgramController {
    @Autowired
    BaseProgramService baseProgramService;


    @GetMapping(value = "index")
    @RequiresPermissions("baseProgram:show")
    public String index(Model model, HttpServletRequest request) {
        return "/system/programManager/index";
    }

    /**
     * @param dto
     * @param model
     * @param page
     * @param limit
     * @Author:zhaoyongke
     * @Description: 查询信息
     * @Date:15:52 2021/1/13
     */
    @GetMapping(value = "showList")
    @ResponseBody
    public ReType showList(BaseProgramDTO dto, Model model, int page, int limit) {
        List<BaseProgramDTO> tList = null;
        Page<BaseProgramDTO> tPage = PageHelper.startPage(page, limit);
        try {
            tList = baseProgramService.selectProgramList(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }
    /**
    * @Author:zhaoyongke
    * @Description: 查询预案
    * @param dto
    * @param model
    * @Date:11:44 2021/1/14
    */
    @GetMapping(value = "queryPro")
    @ResponseBody
    public JsonUtil queryPro(BaseProgramDTO dto, Model model) {
        JsonUtil re = new JsonUtil();
        String keyword = dto.getKeyword();
        if (StringUtil.isBlank(keyword)) {
            re.setMsg("获取预案失败");
            re.setFlag(false);
            return re;
        }
        BaseProgramDTO  dtonew=new BaseProgramDTO();
        List<BaseProgramDTO> tList = baseProgramService.selectProgramList(dtonew);
        for (int i = 0; i < tList.size(); i++) {
            BaseProgramDTO dtoselect= tList.get(i);
            String keywordselect=dtoselect.getKeyword();
            if(StringUtil.isNotBlank(keywordselect)){
                String[] key=keywordselect.split("、");
                for (int j = 0; j < key.length; j++) {
                    String keysz=key[j];
                    if(StringUtil.isNotBlank(keyword)){
                        /*变更原因中包含关键字*/
                        int  isfind= keyword.indexOf(keysz);
                        if(isfind>-1){
                            dto=dtoselect;
                            break;
                        }
                    }

                }
            }

        }
        re.setData(dto);
        re.setFlag(true);
        return re;
    }

    /**
     * 预案管理编辑
     * @param model
     * @param request
     * @param dto
     * @Author:zhaoyongke
     * @Description:
     * @Date:16:01 2021/1/13
     */
    @GetMapping(value = "toupdate")
    @RequiresPermissions("plan:edit")
    public String toupdate(Model model, HttpServletRequest request, BaseProgramDTO dto) {
        String programId = dto.getProgramId();
        BaseProgram baseProgram = baseProgramService.selectByPrimaryKey(programId);
        model.addAttribute("dto", baseProgram);
        return "/system/programManager/update";
    }

    /**
     * 预案管理详情
     * @param model
     * @param request
     * @param dto
     * @Author:zhaoyongke
     * @Description:详情
     * @Date:20:44 2021/1/13
     */
    @GetMapping(value = "detail")
    @RequiresPermissions("plan:detail")
    public String detail(Model model, HttpServletRequest request, BaseProgramDTO dto) {
        String programId = dto.getProgramId();
        BaseProgram baseProgram = baseProgramService.selectByPrimaryKey(programId);
        model.addAttribute("dto", baseProgram);
        return "/system/programManager/detail";
    }

    /**
     * 预案管理新增
     * @param model
     * @param request
     * @param dto
     * @Author:zhaoyongke
     * @Description:
     * @Date:16:01 2021/1/13
     */
    @GetMapping(value = "toadd")
    @RequiresPermissions("plan:add")
    public String addStage(Model model, HttpServletRequest request, BaseProgramDTO dto) {
        return "/system/programManager/add";
    }

    /**
     * 预案管理添加
     * @param dto
     * @Author:zhaoyongke
     * @Description:添加
     * @Date:15:59 2021/1/13
     */
    @PostMapping(value = "add")
    @ResponseBody
    @RequiresPermissions("plan:add")
    public JsonUtil add(BaseProgramDTO dto) {
        JsonUtil re = new JsonUtil();
        try {
            String id = IdUtil.simpleUUID();
            BaseProgram baseProgram = new BaseProgram();
            BeanUtils.copyProperties(dto, baseProgram);
            baseProgram.setProgramId(id);
            baseProgramService.insertSelective(baseProgram);
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("操作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }

    /**
     * 预案管理
     * @param dto
     * @Author:zhaoyongke
     * @Description:修改
     * @Date:15:59 2021/1/13
     */
    @PostMapping(value = "update")
    @ResponseBody
    @RequiresPermissions("plan:edit")
    public JsonUtil update(BaseProgramDTO dto) {
        JsonUtil re = new JsonUtil();
        try {
            String programId = dto.getProgramId();
            BaseProgram baseProgram = baseProgramService.selectByPrimaryKey(programId);
            BeanUtils.copyProperties(dto, baseProgram);
            baseProgram.setModifyBy(CommonUtil.getUserId());
            baseProgram.setModifyDate(DateUtil.getCurrentDateString());
            baseProgramService.updateByPrimaryKey(baseProgram);
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("操作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }

    /**
     *预案管理删除
     * @Author:zhaoyongke
     * @Description:删除
     * @Date:16:00 2021/1/13
     */
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "删除预案")
    @Log(desc = "删除预案", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "delete")
    @ResponseBody
    @RequiresPermissions("plan:delete")
    public JsonUtil delete(BaseProgramDTO dto) {
        if (StringUtils.isEmpty(dto.getProgramId())) {
            return JsonUtil.error("获取数据失败");
        }
        JsonUtil re = new JsonUtil();
        try {
            baseProgramService.deleteByPrimaryKey(dto.getProgramId());
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("操作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }


}
